extends Node

var box=null
var cmd=null
var command=null
var indicator=null
var es=Vector2(0,0)
var ep=Vector2(0,0)
var u=0.1

func _ready():
	pause_mode=Node.PAUSE_MODE_PROCESS

func init():
	box=$"/root/main_scene2d/main_camera2d/cmd"
	cmd=$"/root/main_scene2d/main_camera2d/cmd/cmd"
	command=$"/root/main_scene2d/main_camera2d/cmd/command"
	indicator=$"/root/main_scene2d/main_camera2d/cmd/indicator"
	cmd.add_keyword_color("通过",Color(0,1,0))
	cmd.add_keyword_color("失败",Color(1,0,0))
	cmd.add_keyword_color("挂起",Color(1,1,0))
	if(!command.is_connected("text_changed",self,"text_changed")):
		command.connect("text_changed",self,"text_changed")

func enedit():
	command.readonly=false

func unedit():
	command.readonly=true

func p(s,time=true,ret=true):
	if time:
		cmd.text+=Time.get_time_string_from_system(true)+"|"
	cmd.text+=String(s)
	if ret:
		cmd.text+="\n"
	cmd.set_v_scroll(cmd.get_line_count()-1)

func c():
	cmd.text=""

func s(s):
	cmd.text=String(s)
	cmd.set_v_scroll(cmd.get_line_count()-1)

func open_box(eposition=Vector2(0,0),escale=Vector2(1,1)):
	ep=eposition
	es=escale

func close_box(eposition=Vector2(512,0),escale=Vector2(0,0)):
	ep=eposition
	es=escale

func _process(delta):
	if(box.rect_position!=ep):
		box.rect_position=lerp(box.rect_position,ep,u)
	if(box.rect_scale!=es):
		box.rect_scale=lerp(box.rect_scale,es,u)

func text_changed():
	if(command.get_line_count()>1):
		var cd=""
		for t in command.get_line_count():
			cd+=command.get_line(t)
		command.text=""
		p("user0:"+cd)
		var s0=GDScript.new()
		s0.source_code="func main():\n\t"+cd
		s0.reload(false)
		var i0=s0.new()
		i0.main()
